package com.queen.testretrofitrxjava.internal.di.modules;

import com.queen.testretrofitrxjava.http.MovieService;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by liukun on 16/3/18.
 */
@Module
public class ApiClientModule {

    public static final String BASE_URL = "https://api.douban.com/v2/movie/";

    @Singleton
    @Provides
    MovieService provideMovieService(OkHttpClient okHttpClient){
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        return retrofit.create(MovieService.class);
    }

    @Singleton
    @Provides
    OkHttpClient provideOkHttpClient(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5, TimeUnit.SECONDS);
        return builder.build();
    }
}
